/* 
 * File:   ESResourceManager.cpp
 * Author: Semidán Acosta Betancor
 * 
 * Created on 18 de septiembre de 2011, 9:12
 */

#include <stddef.h>
#include <map>
#include <iterator>
#include <new>
#include <algorithm>
#include <functional>
#include <string.h>


#include "ESResourceManager.h"
#include "ESGameState.h"

namespace ee
{

ESResourceManager* ESResourceManager::resourceManager = NULL;


//TODO Make this singleton double null checked

ESResourceManager* ESResourceManager::instance()
{
    if (resourceManager == NULL)
    {
        resourceManager = new (std::nothrow) ESResourceManager();
    }

    return resourceManager;
}

ESResourceManager::ESResourceManager()
{
}

ESResourceManager::ESResourceManager(const ESResourceManager& orig)
{
}

ESResourceManager::~ESResourceManager()
{
}

sf::Texture* ESResourceManager::loadImage(const std::string& name)
{
    if (imagesResources.find(name) == imagesResources.end())
    {
        std::string path = graphicRootDir + "/" + name;
        sf::Texture *texture = new sf::Texture();
        texture->LoadFromFile(path);
        imagesResources[name] = texture;
    }

    return imagesResources[name];
}

sf::SoundBuffer* ESResourceManager::loadSound(const std::string& name)
{
    if (soundsResources.find(name) == soundsResources.end())
    {
        std::string path = audioRootDir + "/" + name;
        sf::SoundBuffer *sndBuffer = new sf::SoundBuffer();
        sndBuffer->LoadFromFile(path);
        soundsResources[name] = sndBuffer;
    }

    return soundsResources[name];
}

sf::Font* ESResourceManager::loadFont(const std::string& name)
{
    if (fontsResources.find(name) == fontsResources.end())
    {
        std::string path = fontRootDir + "/" + name;
        sf::Font *font = new sf::Font();
        font->LoadFromFile(path);
        fontsResources[name] = font;
    }

    return fontsResources[name];
}

void ESResourceManager::setGraphicRootDir(const std::string& dir)
{
    this->graphicRootDir = dir;
}

void ESResourceManager::setAudioRootDir(const std::string& dir)
{
    this->audioRootDir = dir;
}

void ESResourceManager::setFontRootDir(const std::string& dir)
{
    this->fontRootDir = dir;
}

void ESResourceManager::clearResources()
{
    this->clearGraphicsResources();
    this->clearAudioResources();
    this->clearFontResources();
}

void ESResourceManager::clearGraphicsResources()
{
    if (imagesResources.empty())
    {
        return;
    }

    imagesResources.clear();
    
//    std::map<std::string, sf::Texture*>::iterator it = imagesResources.begin();
//    for (; it != imagesResources.end(); ++it)
//    {
//        //TODO borrar todos los punteros del map
//    }
}

void ESResourceManager::clearAudioResources()
{
    if (soundsResources.empty())
    {
        return;
    }
    
    soundsResources.clear();
//    std::map<std::string, sf::SoundBuffer*>::iterator it = soundsResources.begin();
//    for (; it != soundsResources.end(); ++it)
//    {
//        
//        //TODO borrar todos los punteros del map
//    }
}

void ESResourceManager::clearFontResources()
{
    if (fontsResources.empty())
    {
        return;
    }
    
    fontsResources.clear();

//    std::map<std::string, sf::Font*>::iterator it = fontsResources.begin();
//    for (; it != fontsResources.end(); ++it)
//    {
//        //TODO borrar todos los punteros del map
//    }
}

}